<template>
  <div class="add-address-wrap">
    <van-nav-bar title="新增地址" left-arrow @click-left="onClickLeft" />
    <van-address-edit
      :area-list="areaList"
      show-search-result
      :search-result="searchResult"
      detail-rows="4"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    />
  </div>
</template>

<script>
import { addAddress } from "@/api/address.js";

import areaList from "assets/js/area.js";
import Vue from "vue";
import { NavBar } from "vant";
import { AddressEdit } from "vant";
import { Toast } from "vant";
import { Area } from "vant";

Vue.use(Area);
Vue.use(Toast);
Vue.use(AddressEdit);
Vue.use(NavBar);
export default {
  data() {
    return {
      areaList,
      searchResult: [],
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    async onSave(content) {
      let rs = await addAddress(content);
      if (rs.status === 201) {
        Toast("添加成功");
        this.$router.go(-1);
      } else {
        Toast("添加失败请重试");
      }
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区"
          }
        ];
      } else {
        this.searchResult = [];
      }
    }
  }
};
</script>

<style lang="stylus" scoped>
.add-address-wrap {
  height: 100%;
  background-color: #f9f9f9;

  .van-nav-bar >>> .van-icon {
    color: #0f0f0f !important;
  }

  >>> .van-button--round {
    border-radius: 0px !important;
    border: none;
    outline: none;
  }

  >>> .van-button--danger {
    background-color: #c3a769 !important;
  }
}
</style>